package com.yksj.healthtalk.ui.login;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog.OnClickSureBtnListener;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartControlClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.setting.SettingWebUIActivity;
import com.yksj.healthtalk.utils.SharePreUtils;
import com.yksj.healthtalk.utils.SystemUtils;
import com.yksj.healthtalk.utils.ToastUtil;

public class UserRegisteUI extends BaseFragmentActivity implements OnClickListener{
	
	String mUserId;//用户id
	String mDuomeiNum ;//多美号
	String mPasswd;//密码
	CheckBox mBox;
	Button loginButton;
	EditText mPaswdEditText1;
	EditText mPaswdEditText2;
	
	@Override
	protected void onCreate(Bundle outState) {
		super.onCreate(outState);
		setContentView(R.layout.user_register_ui_layout);
		
		if(outState != null){
			mUserId = outState.getString("mUserId");
			mDuomeiNum = outState.getString("mDuomeiNum");
			mPasswd = outState.getString("mPasswd");
		}
		
		initUI();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("mUserId", mUserId);
		outState.putString("mDuomeiNum", mDuomeiNum);
		outState.putString("mPasswd", mPasswd);
	}
	
	private void initUI(){
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("一键注册");
		
		mBox = (CheckBox)findViewById(R.id.agreement_box);
		mBox.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		loginButton = (Button)findViewById(R.id.ok_btn);
		mPaswdEditText1 = (EditText)findViewById(R.id.passwd1);
		mPaswdEditText2 = (EditText)findViewById(R.id.passwd2);
		mPaswdEditText2.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
			if(actionId== EditorInfo.IME_ACTION_GO){
				createNewUser();
				return true;
			}
				return false;
			}
		});
		loginButton.setOnClickListener(this);
		findViewById(R.id.title_back).setOnClickListener(this);
		
		TextView textView = (TextView)findViewById(R.id.agreement);
		textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		textView.setOnClickListener(this);
	}

	/**
	 * 验证密码是否一致
	 * @return
	 */
	private boolean onPasswdIsRight(){
		String str = mPaswdEditText1.getEditableText().toString();
		if(str.length() < 6){
			SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  "您输入的密码应该是6-12位");
			return false;
		}
		String str2 = mPaswdEditText2.getEditableText().toString();
		if(!str.equals(str2)){
			SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  "密码不一致");
			return false;
		}
		return true;
	}
	
	/**
	 * 创建
	 */
	private void onCreateNewCount(){
		if(!mBox.isChecked()){
			ToastUtil.showShort("您必须同意用户协议与隐私条款!");
			return;	
		}
		if(mDuomeiNum != null){
			onUserProfileSet();
			return;
		}
		mPasswd = mPaswdEditText1.getEditableText().toString();
		loginButton.setClickable(false);
		HttpRestClient.doHttpCreatAccount(SystemUtils.getImeiId(),mPasswd,new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, String content) {
				if (content != null) {
					String[] strs = content.split("&");
					if(strs != null && strs.length >= 2){
						if("0".equals(strs[0])){
							showDialog(strs[1]);
						}else{
							mUserId = strs[0];
							mDuomeiNum = strs[1];
							//设置用户md5
							SmartControlClient client = SmartControlClient.getControlClient();
							client.setUserMd5Id(mUserId);
							client.setUserPassword(mDuomeiNum,mPasswd);
							SharePreUtils.saveUserLoginCache(mDuomeiNum,client.getPassword(), true);
							SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "注册成功, 您的多美号为"+mDuomeiNum,new OnClickSureBtnListener() {
								@Override
								public void onClickSureHander() {
									onUserProfileSet();
								}
							});
						}
					}
				}else{
					showDialog("注册失败");
				}
			}
			@Override
			public void onFinish() {
				super.onFinish();
				loginButton.setClickable(true);
			}
		});
	}
	
	private void showDialog(String str){
		SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),str);
	}
	
	private void onUserProfileSet(){
		Intent intent = new Intent(this,UserProfileSettingUI.class);
		intent.putExtra("userid", mUserId);
		intent.putExtra("duomeinum", mDuomeiNum);
		intent.putExtra("passwd", mPasswd);
		startActivity(intent);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.ok_btn://确定
			createNewUser();
			break;
		case R.id.agreement:
			Intent intent = new Intent(this,SettingWebUIActivity.class);
			intent.putExtra("url",HTalkApplication.getApplication().getDoctorUserAgentPath());
			intent.putExtra("title", "用户协议与隐私条款");
			startActivity(intent);
			break;
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
	private void createNewUser(){
		if(mDuomeiNum != null){
			onCreateNewCount();
		}else{
			SystemUtils.hideSoftBord(getApplicationContext(), mPaswdEditText1);
			if(onPasswdIsRight()){
				onCreateNewCount();
			}
		}
	}
}
